package com.share.device.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.share.common.core.exception.ServiceException;
import com.share.device.service.IMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author YC
 */
@Slf4j
@Service
public class MapServiceImpl implements IMapService {

    @Autowired
    private RestTemplate restTemplate;

    private final String KEY = "4YUBZ-WIZKW-EUYRB-YQS5Y-U4ZBF-ZGFM4";

    @Override
    public Double calculateDistance(String startLongitude, String startLatitude, String endLongitude, String endLatitude) {
//        String url = "https://apis.map.qq.com/ws/direction/v1/walking/?from={from}&to={to}&key={key}";
//        Map<String, String> map = new HashMap<>();
//        map.put("from", startLatitude + "," + startLongitude);
//        map.put("to", endLatitude + "," + endLongitude);
//        map.put("key", KEY);
//        JSONObject result = restTemplate.getForObject(url, JSONObject.class, map);
//        if (result.getIntValue("status") != 0) {
//            String message = result.getString("message");
//            System.out.println("*****===============" + message);
//            throw new ServiceException("地图服务调用失败");
//        }
//        //返回第一条最佳线路
//        JSONObject route = result.getJSONObject("result").getJSONArray("routes").getJSONObject(0);
//        // 单位：米
//        return route.getBigDecimal("distance").doubleValue();

        // 腾讯地图服务调用有限度，这里方便测试，返回固定数（1-100一位小数）
        return 84.2;
    }

}
